<template>
	<div class="front-padding categoryTemplate bg-white relative">
		<div class="tit">借款类别审批单</div>
		<div class="formTable">
			<el-row>
				<el-col :span="3">
					<div class="common">账套名</div>
				</el-col>
				<el-col :span="5">
                    <div class="common grayBackground">{{formData.accountSet}}</div>  
                </el-col>
                <el-col :span="3">
                    <div class="common">公司名字</div>
                </el-col>
                <el-col :span="13">
                    <div class="common grayBackground">{{formData.companyNameChinese}}</div>
                </el-col>
			</el-row>
			<el-row>
                <el-col :span="3">
                    <div class="common">行业</div>               
                </el-col>
                <el-col :span="5">
                    <div class="common grayBackground">{{formData.industryName}}</div>
                </el-col>
                <el-col :span="3">
                    <div class="common">注册号</div>
                </el-col>
                <el-col :span="5">
                    <div class="common grayBackground">{{formData.registrationNumber}}</div>
                </el-col>
                <el-col :span="3">
                    <div class="common">归属人</div>
                </el-col>
                <el-col :span="5">
                    <div class="common grayBackground">{{formData.belonger}}</div>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="3">
                    <div class="common">部门</div>               
                </el-col>
                <el-col :span="5">
                    <div class="common grayBackground">{{formData.deptName}}</div>
                </el-col>
                <el-col :span="3">
                    <div class="common">姓名</div>
                </el-col>
                <el-col :span="5">
                    <div class="common grayBackground">{{formData.empName}}</div>
                </el-col>
                <el-col :span="3">
                    <div class="common">职务</div>
                </el-col>
                <el-col :span="5">
                    <div class="common grayBackground">{{formData.posionName}}</div>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="3">
                    <div class="common required"><span>借款类别</span></div>               
                </el-col>
                <el-col :span="21">
                    <div class="common">
                        <el-input v-model.trim="formData.loanCategory" placeholder="输入借款类别"></el-input>
                    </div>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="3">
                    <div class="common">备注</div>               
                </el-col>
                <el-col :span="21">
                    <div class="common">
                        <el-input v-model.trim="formData.remark"></el-input>
                    </div>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="6">
                    <div class="common"> 一级科目</div>               
                </el-col>
                <el-col :span="6">
                    <div class="common grayBackground">其他应收款</div>
                </el-col>
                <el-col :span="6">
                    <div class="common">二级科目</div>
                </el-col>
                <el-col :span="6">
                    <div class="common" v-if="disabledStatus === 0">{{formData.levelTwoSubjectName}}</div>
                    <div class="common" v-if="disabledStatus === 1">
                        <el-select v-model="formData.levelTwoSubjectId" placeholder="选择二级科目">
                    		<el-option v-for="item in twoSuject" :key="item['id']" :label="item['name']" :value="item['id']"></el-option>
                    	</el-select>
                    </div>
                    <div class="common" v-if="disabledStatus === 2"></div>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="6">
                    <div class="common">三级科目</div>               
                </el-col>
                <el-col :span="6">
                    <div class="common" v-if="disabledStatus === 0">{{formData.levelThreeSubjectName}}</div>
                    <div class="common" v-if="disabledStatus === 1">
                        <el-select v-model="formData.levelThreeSubjectId" placeholder="选择三级科目">
                    		<el-option v-for="item in threeSuject" :key="item['id']" :label="item['name']" :value="item['id']"></el-option>
                    	</el-select>
                    </div>
                    <div class="common" v-if="disabledStatus === 2"></div>
                </el-col>
                <el-col :span="6">
                    <div class="common">四级科目</div>
                </el-col>
                <el-col :span="6">
                    <div class="common" v-if="disabledStatus === 0">{{formData.levelFourSubjectName}}</div>
                    <div class="common" v-if="disabledStatus === 1">
                        <el-select v-model="formData.levelFourSubjectId" placeholder="选择四级科目">
                    		<el-option v-for="item in fourSuject" :key="item['id']" :label="item['name']" :value="item['id']"></el-option>
                    	</el-select>
                    </div>
                    <div class="common" v-if="disabledStatus === 2"></div>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="3">
                    <div class="common">审批人</div>               
                </el-col>
                <el-col :span="6">
                    <div class="common grayBackground"></div>
                </el-col>
                <el-col :span="3">
                    <div class="common">审核意见</div>
                </el-col>
                <el-col :span="12">
                    <div class="common grayBackground"></div>
                </el-col>
            </el-row>
		</div>
		<div class="formTable-btn absolute">
			<el-button type="primary" size="small" v-if="!isSave" disabled>保存</el-button>
			<el-button type="primary" size="small" v-else @click="save">保存</el-button>
			<el-button type="danger" size="small" @click="close">关闭</el-button>
		</div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			formData: {},
			twoSuject: [],
            threeSubject: [],
            fourSubject: [],
            isSave: false,
            rowId: this.$store.state.borrowApproveOrderStore.rowId,
            disabledStatus: 0,          //会计科目操作状态 0 == 可见不可修改 1 == 可见可修改 2 == 不可见不可修改
		}
	},
	watch: {
		'formData.levelOneSubjectId'(newVal, oldVal) {
			if(newVal) this.getTwoSubjectByPid(newVal);
		},
		'formData.levelTwoSubjectId'(newVal, oldVal) {
			if(newVal) this.getThreeSubjectByPid(newVal);
		},
		'formData.levelThreeSubjectId'(newVal, oldVal) {
			if(newVal) this.getFourSubjectByPid(newVal);
        },
        'formData.loanCategory'(newVal) {
            newVal ? this.isSave = true : this.isSave = false;
        }
	},
	methods: {
        getUpdateData() {           //获取修改的单据数据
            this.$http.get('loanCategory/findLoanCategoryById?id=' + this.rowId).then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        this.formData = res.data.data;
                    }
                }
            });
        },
        getFieldPermissions(){  //获取字段权限
            this.$http.get('loanCategory/selectFieldPermissions').then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        this.disabledStatus = res.data.data[0]['tagged'];
                    }
                }
            })
        },
		getData() {               //获取新增时的指定只读数据
			this.$http.get('loanCategory/loadInformation').then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
                        this.formData = res.data.data;
					}
				}
			});
		},
		getTwoSubjectByPid(pid) {			//通过一级科目id获取二级会计科目数据
			this.$http.get('loanCategory/selectLevelsubSubject?id=' + pid).then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
						this.twoSuject = res.data.data;
					}
				}
			});
		},
		getThreeSubjectByPid(pid) {			//通过二级科目id获取三级会计科目数据
			this.$http.get('loanCategory/selectLevelsubSubject?id=' + pid).then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
						this.threeSubject = res.data.data;
					}
				}
			});
		},
		getFourSubjectByPid(pid) {			//通过三级科目id获取四级会计科目数据
			this.$http.get('loanCategory/selectLevelsubSubject?id=' + pid).then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
						this.fourSubject = res.data.data;
					}
				}
			});
		},
		save() {      //保存单据
            let data = {
                accountSet: this.formData['accountSet'],        //账套名
                companyNameChinese: this.formData['companyNameChinese'],        //公司名称
                industry: this.formData['industry'],            //行业
                registrationNumber: this.formData['registrationNumber'],        //注册号
                belonger: this.formData['belonger'],            //归属人
                dept: this.formData['dept'],                    //部门
                emp: this.formData['emp'],                      //姓名
                posion: this.formData['posion'],                //职务
                loanCategory: this.formData['loanCategory'],    //借款类别
                remark: this.formData['remark'],                //备注
                levelOneSubjectId: this.formData['levelOneSubjectId'],      //一级科目
                levelTwoSubjectId: this.formData['levelTwoSubjectId'],      //二级科目
                levelThreeSubjectId: this.formData['levelThreeSubjectId'],  //三级科目
                levelFourSubjectId : this.formData['levelFourSubjectId'],   //四级科目
                approver: this.formData['approver'],            //审批人
                approvalOpinion: this.formData['approvalOpinion'],      //审批意见
            }
            if(!data['loanCategory']) {
                this.$message({message: '必填项不能为空', type: 'warning', duration: 1500});
                return false;
            } else {
                if(this.formData['id']) {
                    //修改
                    data['id'] = this.formData['id'];
                    this.$http({
                        url: 'loanCategory/updateLoanCategory',
                        method: 'post',
                        params: data
                    }).then(res => {
                        if(res.data.code == 20001) {
                            this.$message({type: 'success', duration: 1500, message: '单据修改成功'});
                            setTimeout(() => {
                                this.$store.dispatch('borrowApproveOrderStore/getOrderTableDataByBtnStutas');
                                this.close();
                            }, 1000);
                        }
                    });
                } else {
                    let nameArray = ['levelOneSubjectId', 'levelTwoSubjectId', 'levelThreeSubjectId', 'levelFourSubjectId'],
                        newArr = [];
                    nameArray.forEach(item => {
                        if(data[item]) {
                           newArr.push(data[item]); 
                        }
                    });
                    //新增
                    this.$http({
                        url: 'loanCategory/insertLoanCategory',
                        method: 'post',
                        params: data
                    }).then(res => {
                        if(res.data.code == 20001) {
                            //单据新增成功后要开启审批流程
                            let obj = {
                                bill: 1,
                                department: data['dept'],
                                employee: data['emp'],
                                verchar: newArr,
                                billDateId: res.data.data,       //添加单据成功后的单据id
                            }
                            //开启审批流程
                            this.startApprove(obj);
                        }
                    });
                }
            }
		},
        startApprove(obj) {           //开启审批流程
            this.$http.post('activiti/approveStart', JSON.stringify(obj), {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }).then(res => {
                if(res.data.code == 20001) {
                    this.$message({type: 'success', duration: 1500, message: '单据新增成功'});
                    setTimeout(() => {
                        this.$store.dispatch('borrowApproveOrderStore/getOrderTableDataByBtnStutas');
                        this.close();
                    }, 1000);
                }
            });
        },
		close() {
			let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
		}
	},
	mounted() {
        this.getFieldPermissions();
        if(this.rowId) {
            this.getUpdateData();
        } else {
            this.getData();
        }
	}
}
</script>